/**
 *
 */
package wsepForum.ui.jaxcent.states;

import java.util.Map;

import jaxcent.HtmlInputText;
import jaxcent.Jaxception;

import wsepForum.ui.jaxcent.IXContext;

/**
 * @author Oren
 *
 */
public class XUserRegister extends AXUserControl {

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public XUserRegister(IXContext context) {
		super(context);
	}

	/*****************************
	 * METHODS                   *
	 *****************************/
	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.AXUserControl#initContent()
	 */
	protected void initContent() {
		super.initContent();
		try {
			new HtmlInputText (this.context.getPage(),"xusername").focus();
		} catch (Jaxception e) {/* ignore failure in focusing...*/}
	}

	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.XUserEdit#construct()
	 */
	@Override
	protected void construct() {
		String newPassStr,title,oldPasswordPanel,submitText,userField;
		title = "Register a new user";
		newPassStr = "Password";
		oldPasswordPanel = "";
		userField = "<input type=\"text\" tabindex=\"1\" name=\"xusername\" id=\"xusername\"  size=\"30\" value=\"\" class=\"inputbox autowidth\" /><br /><span>Must be 5 to 30 length.</span>";
		submitText = "Register";
		construct(newPassStr,title,oldPasswordPanel,submitText,userField);
	}

	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.AXUserControl#dealWithSubmit(java.util.Map, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	protected void dealWithSubmit(Map pageData, String newpassword) {
	      try {
				String userName = (String) pageData.get( "xusername" );
				if ( userName.equals("") || userName.length() < 5) {
		        	this.context.getPage().showMessageDialog( "Please enter username with more than 4 letters" );
		            return;
		        }
				if (this.context.getSession().getHandler().register(userName, newpassword)) {
					this.context.loginUser(userName, newpassword);
				} else {
					this.context.getPage().showMessageDialog( "User name already taken or illegal input insert,\nplease try again." );
					return;
				}
			} catch (Exception e) {
				this.context.getPage().showMessageDialog("Error in the registration procces!\n"+e.getMessage());
				return;
			}
	}

}
